package com.gewu.advertisement.core.model

import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.annotation.JsonTypeInfo
import com.fasterxml.jackson.annotation.JsonTypeName

/**
 * 平台WebSocket通讯协议包
 * @author htt 2021/1/7
 */
@JsonTypeInfo(
    use = JsonTypeInfo.Id.NAME,
    include = JsonTypeInfo.As.PROPERTY,
    property = "action",
    defaultImpl = GewuPacket.Unknown::class,
    visible = true
)
internal sealed class GewuPacket<T> {

    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    var action: Int? = null
    abstract val data: T?

    // ------ 客户端 -> 服务端 ------

    //心跳上报
    @JsonTypeName("2000")
    class HeartBeatUpload(override val data: String = "HB") : GewuPacket<String>()

    //广告播放状态上报
    @JsonTypeName("2001")
    class AdPlayStateUpload(override val data: List<AdPlayState> = emptyList()) : GewuPacket<List<AdPlayState>>()


    // ------ 服务端 -> 客户端 ------

    //心跳回复
    @JsonTypeName("1000")
    class HeartBeatReply(override val data: String? = null) : GewuPacket<String>()

    //下发广告
    @JsonTypeName("1001")
    class AdUpdate(override val data: List<Advertising> = emptyList()) :
        GewuPacket<List<Advertising>>()

    //播放控制
    @JsonTypeName("1002")
    class PlayController(override val data: PlayControl? = null) : GewuPacket<PlayControl>()


    // 未知命令
    @JsonTypeName("")
    class Unknown(override val data: Any? = null) : GewuPacket<Any>()

    override fun toString(): String {
        return "${this.javaClass.simpleName}(action=$action, data=$data)"
    }

}

